package com.lenovo.smartengine.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import com.dolby.api.DsAccess;
import com.dolby.api.DsConstants;
import com.dolby.api.DsGlobalEx;
import com.dolby.api.IDsAccessEvents;
import com.dolby.api.IDsEvents;
import com.lenovo.smartengine.receiver.IncomingAudioTypeReceiverEngine;
import com.lenovo.smartengine.utils.AudioTypeSetUtils;
import com.lenovo.smartengine.utils.Constants;

public class SmartEngineService extends Service implements IDsAccessEvents,
        IDsEvents {
    private DsGlobalEx mDsClient = null;
    private IntentFilter filter = null;
    private IncomingAudioTypeReceiverEngine AudioTypeReceiver = null;

    @Override
    public void onCreate() {
        AudioTypeSetUtils.Logi(Constants.TAG, "Service-onCreate");
        super.onCreate();
        Context context = this.getApplicationContext();
        try {
            if (mDsClient == null) {
                mDsClient = new DsGlobalEx();
                // bind to DsService
                boolean isSuccess = mDsClient.registerClient(context, this);
                AudioTypeSetUtils.Logi(Constants.TAG, "register DSClient :"
                        + isSuccess);
            }
        } catch (Exception e) {
            mDsClient = null;
        }

        filter = new IntentFilter("com.lenovo.action.audiotype");
        AudioTypeReceiver = new IncomingAudioTypeReceiverEngine(mDsClient);

        AudioTypeSetUtils.Logi(Constants.TAG, "register AudioTypeReceiver ...");
        registerReceiver(AudioTypeReceiver, filter);
        AudioTypeSetUtils.Logi(Constants.TAG,
                "register AudioTypeReceiver success...");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "Service-onStartCommand");
        long service_start = intent.getLongExtra("service_start", 0);
        long service_started = System.currentTimeMillis();
        AudioTypeSetUtils.Logi(Constants.TAG,
                "Service started...time consumed:"
                        + (service_started - service_start) + "ms");
        /*
         *  * 这里返回状态有三个值，分别是:
         * 1、START_STICKY：默认值，当服务进程在运行时被杀死，系统将会把它置为started状态，但是不保存其传递的Intent对象
         * ，之后，系统会尝试重新创建服务;
         * 2、START_NOT_STICKY：当服务进程在运行时被杀死，并且没有新的Intent对象传递过来的话，
         * 系统将会把它置为started状态， 但是系统不会重新创建服务，直到startService(Intent intent)方法再次被调用;
         * 3、START_REDELIVER_INTENT：当服务进程在运行时被杀死，它将会在隔一段时间后自动创建，
         * 并且最后一个传递的Intent对象将会再次传递过来。
         */
        // return super.onStartCommand(intent, flags, startId);
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        AudioTypeSetUtils.Logi(Constants.TAG, "Service-onBind");
        return null;
    }

    @Override
    public void onDestroy() {
        AudioTypeSetUtils.Logi(Constants.TAG, "Service-onDestroy");
        if (AudioTypeReceiver != null) {
            unregisterReceiver(AudioTypeReceiver);
        }
        super.onDestroy();
    }

    @Override
    public void onAccessAvailable() {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onAccessAvailable()");
        if ((null != mDsClient)
                && (DsAccess.THIS_APP_GRANTED != mDsClient.checkAccessRight())) {
            if (DsConstants.DS_NO_ERROR != mDsClient.requestAccessRight()) {
                AudioTypeSetUtils.Logi(Constants.TAG,
                        "Fail to get access right");
                return;
            }
        }
        AudioTypeSetUtils.Logi(Constants.TAG, "Got access right");
    }

    @Override
    public void onAccessForceReleased(String arg0, int arg1) {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onAccessForceReleased()");
    }

    @Override
    public boolean onAccessRequested(String arg0, int arg1) {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onAccessRequested()");
        return false;
    }

    @Override
    public void onClientConnected() {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onClientConnected()");

        // register Ds Event listener
        mDsClient.registerDsEvents(this);
        AudioTypeSetUtils.Logi(Constants.TAG, "Register DsEvent listener");

        int result = mDsClient.requestAccessRight();
        if (result != DsConstants.DS_NO_ERROR) {
            AudioTypeSetUtils.Logi(Constants.TAG,
                    "onClientConnected() request audio focuse failed, result = "
                            + result);
            // DS2API-TODO: ......??? When and Where should application request
            // audio focus again???
            return;
        } else {
            AudioTypeSetUtils.Logi(Constants.TAG,
                    "onClientConnected() request audio focuse successfully!");
        }
        // Client has connected.
    }

    @Override
    public void onClientDisconnected() {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onClientDisconnected()");
    }

    @Override
    public void onDsOn(boolean on) {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onDsOn(" + on + ")");
    }

    @Override
    public void onDsSuspended(boolean isSuspended) {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onDsSuspended(" + isSuspended
                + ")");
    }

    @Override
    public void onProfileSelected(int profile) {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onProfileSelected(" + profile
                + ")");
    }

    @Override
    public void onProfileSettingsChanged(int profile) {
        // TODO Auto-generated method stub
        AudioTypeSetUtils.Logi(Constants.TAG, "onProfileSettingsChanged("
                + profile + ")");
    }
}
